<?php

class Email
{
	private $error;
	public $cookie_jar1;
	public function __construct()
	{}
	
	public function __destruct()
	{
		$this->clean();
	}

	public function get_contacts($account, $password)
	{
		$account = current(explode('@', $account));
		$account = strtolower(trim($account));
		$password = trim($password);
		if (empty($account) || empty($password)) {
			$this->error = 'err_01';
			return false;
		} else {
			$account = $account . '@sohu.com';
			$password = md5($password);
		}

		$tmp_dir = PATH_SEPARATOR == ':' ? '/dev/shm/invite/' : 'c:/windows/temp/';
		$tmp_dir = $tmp_dir . substr(md5(microtime(true) . rand(0, 999999999)), 0, 2);

		$cookie_jar1 = tempnam($tmp_dir, 'cookie1');
		$this->cookie_jar1 = $cookie_jar1;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		curl_setopt($ch, CURLOPT_URL, 'http://passport.sohu.com/sso/login.jsp?userid=' . urlencode($account) . '&password=' . $password . '&appid=1000&persistentcookie=0&s=1219973284046&b=1&w=1440&pwdtype=1');
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		$return = trim(curl_exec($ch));
		curl_close($ch);

		if ($return != "login_status='success';") {
			$this->error = 'err_01';
			return false;
		}

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		curl_setopt($ch, CURLOPT_URL, 'http://login.mail.sohu.com/servlet/LoginServlet');
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar1);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		$return = curl_exec($ch);
		curl_close($ch);

		$lines = explode("\n", $return);
		$location = '';
		foreach ($lines as $line) {
			if (preg_match('/^Location:/', $line)) {
				$location = substr($line, 9);
				$location = trim($location);
			}
		}

		if (!preg_match('/^http:\/\/mail\.sohu\.com\/bapp\/(\d+)\/main$/', $location, $m)) {
			$this->error = 'err_01';
			return false;
		}

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		//curl_setopt($ch, CURLOPT_URL, 'http://mail.sohu.com/bapp/' . $m[1] . '/contact');
		curl_setopt($ch, CURLOPT_URL, 'http://mail.sohu.com/address/completelist?' . rand(0, 999999999));
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		$return = curl_exec($ch);
		curl_close($ch);
		
		$return = str_replace('var _otherAddrDataLoaded=\'', '', $return);
		$return = trim($return, ';');
		//var_dump($return);

		preg_match_all('/"nickname"\: "(.*?)"/', $return, $m1);
		preg_match_all('/"email"\: "(.*?)"/', $return, $m2);
		
		if (!count($m1[1]) || !count($m2[1])) {
			$this->error = 'err_02';
			return false;
		}
		
//		foreach ((array) $result->listString as $contact) {
//			$tmp = array();
//			$tmp['account'] = iconv('UTF-8', 'GBK', $contact->email);
//			$tmp['nickname'] = iconv('UTF-8', 'GBK', $contact->name);
//			array_push($contacts, $tmp);
//		}
		
		$contacts = array();
		foreach ($m1[1] as $k => $v) {
			$tmp = array();
			$tmp['email'] = iconv('UTF-8', 'GBK', $m2[1][$k]);
			$tmp['name'] = iconv('UTF-8', 'GBK', $m1[1][$k]);
			array_push($contacts, $tmp);
		}
		if (empty($contacts)) {
			$this->error = 'err_02';
			return false;
		}
		
		return $contacts;
	}

	public function get_error()
	{
		return $this->error;
	}
	
	public function clean()
	{
		if(file_exists($this->cookie_jar1))
		{
			@unlink($this->cookie_jar1);
		}
	}
}

?>